
import { _decorator, Component, Node, NodePool, Size, size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { Singleton } from '../../tools/Singleton';
import { Loader } from '../../tools/LoadManger';
import { Move } from '../component/Move';
import { BulletNormal } from './BulletNormal';
import { DataHelper } from '../../system/DataHelper';
import { tasteMainGun_config } from '../../manager/AppControll';
import { BulletMissile } from './BulletMissile';
import { BulletDouble } from './BulletDouble';
import { resManger } from '../../tools/ResManger';
const { ccclass, property } = _decorator;


enum TasteGunType {
    DoubleShot = 90001,
    Missile = 90002,
}

@ccclass('BulletManager')
export class BulletManager extends Singleton<BulletManager>{
    public ndBullet: Node = null;
    public ltBullet: Node[] = [];
    public normalBulletPool: NodePool;
    public doubleBulletPool: NodePool;
    public missileBulletPool: NodePool;


    InitBulletManager(node: Node) {
        this.normalBulletPool = new NodePool();
        for (let i = 0; i < 40; i++) {
            let node = new Node();
            node.addComponent(Sprite);
            node.addComponent(BulletNormal);
            node.addComponent(Move)
            this.normalBulletPool.put(node);
        }
        this.doubleBulletPool = new NodePool();
        for (let i = 0; i < 15; i++) {
            let node = new Node();
            node.addComponent(Sprite);
            node.addComponent(Move)
            node.addComponent(BulletDouble)
            this.doubleBulletPool.put(node);
        }
        this.missileBulletPool = new NodePool();
        for (let i = 0; i < 15; i++) {
            let node = new Node();
            node.addComponent(Sprite);
            node.addComponent(Move);
            node.addComponent(BulletMissile)
            this.missileBulletPool.put(node);
        }
        this.ndBullet = node;
    }

    CreateBullet(id: number, eulerAngles: Vec3, startV3: Vec3, endPoint: Vec3) {

        let bulletConfig = DataHelper.GetBulletConfig(id);
        
        resManger.GetBulletSpf("bullet/" + bulletConfig.rightPosition, (spf) => {
            let node = this.normalBulletPool.get();
            if (!node) {
                console.log("normalBulletPool池存量不足")
                return;
            }
            node.getComponent(Sprite).spriteFrame = spf;
            node.getComponent(Sprite).trim = true;
            node.position = new Vec3(startV3.x, startV3.y, 0);
            node.eulerAngles = this.RevisioEuler(eulerAngles.clone());
            node.active = true;
            node.layer = 1 << 25;
            node.getComponent(UITransform).contentSize = new Size(bulletConfig.bulletSize[0], bulletConfig.bulletSize[1]);
            this.ndBullet.addChild(node);
            node.getComponent(Move).InitPoint(new Vec2(startV3.x, startV3.y), new Vec2(endPoint.x, endPoint.y), bulletConfig.moveSpeed);

        })
    }

    CreateTasterBullet(id: number, eulerAngles: Vec3, startV3: Vec3, endPoint: Vec2, config: tasteMainGun_config, target: Node) {
        let bulletConfig = DataHelper.GetBulletConfig(id);
        resManger.GetBulletSpf("bullet/" + bulletConfig.rightPosition, (spf) => {
            let node = null
            if (config.tasteId == TasteGunType.DoubleShot) {
                node = this.doubleBulletPool.get();
                if (!node) {
                    console.log("doubleBulletPool池存量不足")
                    return;
                }
                node.getComponent(BulletDouble).InitConfig(config, target);
            } else if (config.tasteId == TasteGunType.Missile) {
                node = this.missileBulletPool.get();
                if (!node) {
                    console.log("missileBulletPool池存量不足")
                    return;
                }
                node.getComponent(BulletMissile).InitConfig(config, target);
            }
            node.getComponent(Sprite).spriteFrame = spf;
            node.getComponent(Sprite).trim = true;
            node.position = new Vec3(startV3.x, startV3.y, 0);
            node.eulerAngles = this.RevisioEuler(eulerAngles.clone());
            node.active = true;
            node.layer = 1 << 25;
            node.getComponent(UITransform).contentSize = new Size(bulletConfig.bulletSize[0], bulletConfig.bulletSize[1]);
            this.ndBullet.addChild(node);
            node.getComponent(Move).InitPoint(new Vec2(startV3.x, startV3.y), endPoint, bulletConfig.moveSpeed);

        })
    }

    RevisioEuler(eulerAngles: Vec3) {
        if (eulerAngles.z <= 90 && eulerAngles.z >= 0) {
            eulerAngles.add(new Vec3(0, 0, -90));
        } else if (eulerAngles.z <= 0 && eulerAngles.z >= -90) {
            eulerAngles.add(new Vec3(0, 0, 90));
        }
        return eulerAngles;
    }
}

export var bulletManger = BulletManager.Instance<BulletManager>()